<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加 -->

    <el-card>
      <el-row :gutter="20">
        <el-col :span="14">
          <el-button type="primary" @click="showAddCateList">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        show-index
        index-text="#"
        border
        style="width: 100%"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="!scope.row.cat_deleted===false"
            style="color:lightgreen;"
          ></i>
          <i class="el-icon-error" v-else style="color:red;"></i>
        </template>
        <!--排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="success" v-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="warning" v-if="scope.row.cat_level===2">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button class="el-icon-edit" type="success">编辑</el-button>
          <el-button class="el-icon-delete" type="warning">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 添加分类 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateDialogVisible"
        @close="addCateDialogClose"
        width="50%"
      >
        <el-form
          :model="addCateForm"
          :rules="addCateRules"
          ref="addCateRef"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              expand-trigger="hover"
              v-model="selectedKeys"
              :options="ParentCateList"
              :props="cascaderProps"
              @change="ParentCateChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //商品分类列表
      cateList: [],
      queryInfo: {
        type: 3,
        pagenum: 3,
        pagesize: 5
      },
      total: 0,
      addCateDialogVisible: false,
      columns: [
        { prop: 'cat_name', label: '分类名称' },
        //将当前列定义为模板列
        {
          prop: 'cat_deleted',
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          prop: 'cat_level',
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateRules: {
        cat_name: [{ required: true, message: '请输入分类名', trigger: 'blur' }]
      },
      addCateForm: { cat_pid: 0, cat_name: '', cat_level: 0 },
      //得到的数据
      ParentCateList: [],
      //指定选择器的配置对象
      cascaderProps: {
        //选中的绑定数据是id
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
        // expandTrigger: 'hover'
      },
      //保存的两极选项
      selectedKeys: []
    }
  },
  methods: {
    addCateDialogClose() {
      //当关闭添加分类对话框时，重置表单
      this.$refs.addCateRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      this.cateList = res.data.result
      console.log(res.data.result)
      this.total = res.data.total
    },
    //显示添加分类

    showAddCateList() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },

    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },

    handleCurrentChange(num) {
      this.queryInfo.pagenum = num
      this.getCateList()
    },
    //数据显示
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) return this.$message.error('失败')
      this.ParentCateList = res.data
      console.log(this.ParentCateList)

      this.getCateList()
    },
    ParentCateChange() {
      console.log(this.selectedKeys)
      //如果用户选择了父级分类
      if (this.selectedKeys.length > 0) {
        //则将数组中的最后一项设置为父级分类
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ]
        return
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
        return
      }
    },
    //提交分类
    async addCate() {
      console.log(this.addCateForm)
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          `categories`,
          this.addCateForm
        )
        console.log(res)

        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    }
    //对话框关闭
  },
  created() {
    this.getCateList()
  }
}
</script>
<style lang="less" scoped>
.treeTabel {
  margin: 7px;
}
.el-cascader {
  width: 100%;
}
.el-cascader-menu {
  height: 240px;
}
</style>